package com.suishoupai.xmb.controller;

import com.github.pagehelper.Page;
import com.suishoupai.xmb.entity.AjaxMessage;
import com.suishoupai.xmb.entity.Blacklist;
import com.suishoupai.xmb.entity.User;
import com.suishoupai.xmb.exception.XmbException;
import com.suishoupai.xmb.service.BlacklistService;
import com.suishoupai.xmb.service.UserService;
import com.suishoupai.xmb.utils.PageUtil;
import com.suishoupai.xmb.utils.ValidateUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by caojian on 2017/4/18.
 */
@Controller
@RequestMapping("/blacklist")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class BlacklistController {
    @Autowired
    private BlacklistService blacklistService;
    @Autowired
    private UserService userService;

    /**
     * 黑名单列表
     *
     * @param model
     * @return
     */
    @GetMapping("")
    public Object index(@RequestParam Map map, Model model) {
        PageUtil.startPage(map);
        Page page = (Page) blacklistService.getAll();
        model.addAttribute("blacklists", page.toPageInfo());
        return "blacklist-list";
    }


    /**
     * 添加黑名单页面
     *
     * @param model
     * @return
     */
    @GetMapping("/add")
    public Object addPage(@RequestParam Map map, Model model) {
        Long userId = MapUtils.getLong(map, "userId", null);
        if (userId != null) {
            User user = userService.getUserById(userId);
            if (user == null) {
                model.addAttribute("message", "没有找到id为'" + userId + "'的用户。");
            } else {
                model.addAttribute("user", user);
                model.addAttribute("isBlacklist", blacklistService.isExist(userId));
            }
        }
        return "blacklist-add";
    }

    /**
     * 添加进黑名单
     *
     * @return
     */
    @PostMapping("")
    @ResponseBody
    public Object add(Blacklist blacklist) {
        ValidateUtil.notNull(blacklist.getUserId());
        ValidateUtil.notNull(blacklist.getDescription());
        try {
            blacklistService.add(blacklist);
        } catch (XmbException e) {
            if (e.isRepeatRequest()) {
                return AjaxMessage.newSuccessMsg();
            }
            return AjaxMessage.newErrorHintMsg(e.getError());
        }
        return AjaxMessage.newSuccessMsg();
    }

    /**
     * 移除黑名单
     *
     * @return
     */
    @PostMapping("/{userId}")
    @ResponseBody
    public Object delete(@PathVariable("userId") long userId) {
        try {
            blacklistService.del(userId);
        } catch (XmbException e) {
            if (e.isRepeatRequest()) {
                return AjaxMessage.newSuccessMsg();
            }
            return AjaxMessage.newErrorHintMsg(e.getError());
        }
        return AjaxMessage.newSuccessMsg();
    }
}
